//https://leetcode.cn/problems/maximum-subarray/
package codeRandomThoughts.Test53最大子序和;

public class Solution3 {
    public int maxSubArray(int[] nums) {
        //以nums[i]为结尾的连续子数组的最大和
        int[] dp = new int[nums.length];

        dp[0] = nums[0];
        int maxLen = dp[0];

        for (int i = 1; i < nums.length; i++) {
            if (dp[i - 1] > 0) {
                dp[i] = nums[i] + dp[i - 1];
            } else {
                dp[i] = nums[i];
            }

            maxLen = Math.max(dp[i], maxLen);
        }

        return maxLen;
    }
}
